package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.Crole;
import com.zr.model.Cuser;
import com.zr.service.UpdateUserService;

@Controller
public class UpdateUserAction extends ActionSupport implements ServletResponseAware,SessionAware,ServletRequestAware{
	 private HttpServletResponse resp;
	 private  Map<String,Object> session; 
	 private HttpServletRequest req;	

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.resp = response;
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}
	
	@Resource
	private UpdateUserService updateUserService;
	
	public void updateUser(){
		resp.setCharacterEncoding("utf8");
		String uids = req.getParameter("uid");
		int uid = Integer.parseInt(uids);
		String ustate = req.getParameter("ustate");
		String rname = req.getParameter("rname");
		String uname = req.getParameter("uname");
		Cuser currentUser = (Cuser)session.get("cuser");
		String cuname = currentUser.getUname();
		int cuid = currentUser.getUid();
		System.out.println(updateUserService.getChangedOrNot(rname, ustate, uid)==0);
		try {
			PrintWriter pw = resp.getWriter();
			if(updateUserService.getChangedOrNot(rname, ustate, uid)==1){
				updateUserService.updateUserByInfo(uid, ustate, uname, cuid, cuname, rname);
				pw.write("1");
				pw.flush();
				pw.close();
			}else if (updateUserService.getChangedOrNot(rname, ustate, uid)==0) {
				System.out.println("same");
				pw.write("2");
				pw.flush();
				pw.close();
			} else {
				pw.write("0");
				pw.flush();
				pw.close();
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	

}
